//角色管理模块
import axios from 'axios'

const $host = 'http://132.232.110.185/personnelManagement'
export default {
    namespaced: true,
    actions: {
        //修改请求
        async updateRoleInfo(ctx, obj) {
            console.log(obj)
            await axios({
                method: 'POST',
                url: `${$host}/role/editRole`,
                headers: {
                    'Content-Type': 'application/json'
                },
                data: {
                    rDescription: obj.rDescription,
                    rName: obj.rName,
                    roles: obj.roles
                }
            }).then(res => {
                ctx.commit('updateRole')
            })
        },

        //添加请求
        async addRoleInfo(ctx, obj) {
            console.log(obj)
            await axios({
                method: 'POST',
                url: `${$host}/role/addRole`,
                headers: {
                    'Content-Type': 'application/json'
                },
                data: {
                    rDescription: obj.rDescription,
                    rName: obj.rName,
                    roles: obj.roles
                }
            }).then(res => {
                ctx.commit('addRole')
            })
        },

        // 显示权限
        async getRolesInfo(ctx, obj) {
            await axios({
                method: 'POST',
                url: `${$host}/role/showRes`,
                params: {
                    rId: obj.rId
                }
            }).then(res => {
                console.log(res)
                ctx.commit('handleRoleEdit', res.data)
            })
        }
    },
    mutations: {
        //关闭模态框
        closeDialog(state) {
            if (state.formLabelAlign.roleAddFormVisible) state.formLabelAlign.roleAddFormVisible = false
            if (state.formLabelAlign.roleEditFormVisible) state.formLabelAlign.roleEditFormVisible = false
        },
        //修改角色管理dialog界面渲染
        handleRoleEdit(state, row) {
            state.updateRoles = {...row, checkList: row.resName}
            state.formLabelAlign.roleEditFormVisible = true
        },
        //修改角色资料
        updateRole(state) {
            state.formLabelAlign.roleEditFormVisible = false
            console.log('修改成功')
        },
        //显示添加dialog
        showRoleAddDialog(state) {
            state.addStatus = false
            state.formLabelAlign.roleAddFormVisible = true
            state.addRoles = {}
        },
        //获取checkbox中选中的值
        changeCheckbox(state, value) {
            if (state.formLabelAlign.roleAddFormVisible) state.addRoles.roles = value
            if (state.formLabelAlign.roleEditFormVisible) state.updateRoles.roles = value
        },
        //添加角色回调
        addRole(state) {
            state.addStatus = true
            state.formLabelAlign.roleAddFormVisible = false
            console.log('添加成功')
        }
    },
    state: {
        formLabelAlign: {
            rName: '',
            rNote: '',
            roles: ['系统管理', '绩效评分', '员工管理', '部门管理', '人才储备', '月度目标', '相关活动', '数据统计'],
            checkList: [],
            roleAddFormVisible: false,
            roleEditFormVisible: false,
        },
        addRoles: {},
        updateRoles: {},
        //添加状态
        addStatus: false,
    },
    getters: {}
}